#ifndef ROTATEBOTHAXISES_H
#define ROTATEBOTHAXISES_H

#include "geometry/math3d.h"
#include "geometry/vector3d.h"

GLfloat *dirXLocal = Math3D::newInstanceWithValue(0.0f, 0.0f, 0.0f);
GLfloat *dirYLocal = Math3D::newInstanceWithValue(0.0f, 0.0f, 0.0f);

void rotateSingleAxisNew(double angleInRadian, GLfloat *axis, GLfloat *direction,
                         GLfloat *returnedValue) {
    if (Math3D::dotNew(axis, direction) != 0.0f) return;


    Math3D::normalizeWithoutChanging(direction, dirXLocal);
    Math3D::crossVectorsNew(axis, dirXLocal, dirYLocal);
    Math3D::normalizeWithChanging(dirYLocal);

    double cosValue = cos(angleInRadian);
    double sinValue = sin(angleInRadian);

    Math3D::multiplyVector(cosValue, dirXLocal);
    Math3D::multiplyVector(sinValue, dirYLocal);

    Math3D::setNewValue(0.0f, 0.0f, 0.0f, returnedValue);
    Math3D::addSingleVectorNew(dirXLocal, returnedValue);
    Math3D::addSingleVectorNew(dirYLocal, returnedValue);
}

Vector3D *rotateAxis(double angleInRadian, Vector3D *axis, Vector3D *direction) {
    if (Math3D::dot(axis, direction) != 0) return NULL;

    Vector3D *directionX = direction->normalize();
    Vector3D *directionY = Math3D::crossVectors(axis, directionX)->normalize();

    QList<Vector3D*> *vectors = new QList<Vector3D*>();
    vectors->push_back(directionX->multiply(cos(angleInRadian)));
    vectors->push_back(directionY->multiply(sin(angleInRadian)));

    return Math3D::addVectors(vectors);
}


/** 旋转两个坐标轴，为获取旋转矩阵提供方便（第二版），这次将直接引用现有的数组 */
void rotateBothAxiesNew(double angleInRadian, GLfloat *axis, GLfloat *direction1, GLfloat *direction2) {
    rotateSingleAxisNew(angleInRadian, axis, direction1, direction1);
    rotateSingleAxisNew(angleInRadian, axis, direction2, direction2);
}

/** 旋转两个坐标轴，为获取旋转矩阵提供方便 */
QList<Vector3D*> *rotateBothAxises(double angleInRadians, Vector3D *axis, QList<Vector3D*> *directions) {
    QList<Vector3D*> *axises = new QList<Vector3D*>();
    Vector3D *dir1New = rotateAxis(angleInRadians, axis, directions->at(0));
    Vector3D *dir2New = rotateAxis(angleInRadians, axis, directions->at(1));

    axises->push_back(dir1New); axises->push_back(dir2New);
    return axises;
}

#endif // ROTATEBOTHAXISES_H
